package com.example.cxx.zhuce;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.TextView;

public class Exam extends Activity {

    private String str="";
    private Button btn;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_exam);

        final Button btn=findViewById(R.id.btn_sub);
        final TextView choose=(TextView) findViewById(R.id.tv_choose);

        CheckBox cbOne=(CheckBox) findViewById(R.id.cb_1);
        CheckBox cbTwo=(CheckBox) findViewById(R.id.cb_2);
        CheckBox cbThree=(CheckBox) findViewById(R.id.cb_3);
        CheckBox cbFour=(CheckBox) findViewById(R.id.cb_4);

        cbOne.setOnCheckedChangeListener(new MyListener());
        cbTwo.setOnCheckedChangeListener(new MyListener());
        cbThree.setOnCheckedChangeListener(new MyListener());
        cbFour.setOnCheckedChangeListener(new MyListener());

        //找到页面中的按钮 //choose.setText("你的选择是："+str);
        Button btn1=findViewById(R.id.btn_sub);
        //处理提交的数据
        btn1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //点击按钮的时候，弹出对话框，提示确定或者取消

                //第一步，声明AlertDialog对象
                AlertDialog dialog;
                //第二步，创建Builder对象，并且设置按钮实现功能
                AlertDialog.Builder builder=new AlertDialog.Builder(Exam.this);
                builder.setTitle("确定交卷吗？");
                //在弹出框上添加两个按钮
                builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();//将当前弹出框关闭
                        choose.setText("你的选择是："+str);
                        Log.i("main","确定按钮被点击了");
                    }
                });
                builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                        Log.i("main","取消按钮被点击了");
                    }
                });
                //第三步，为AlertDialog对象赋值
                dialog=builder.create();
                //将弹出框显示出来
                dialog.show();
            }
        });
    }

    private class MyListener implements CompoundButton.OnCheckedChangeListener {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            String r=buttonView.getText().toString();
            //str=str+r;
            if(isChecked){
                if (!str.contains(r)){
                    str=str+" "+r;
                }
            }else{
                    if (str.contains(r)){
                        str=str.replace(""+r,"");
                    }
                }
                Log.i("exam",str);
            }
        }

    }

